import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { AppPaginationComponent, PaginationModel } from "../../../../../widgets/pagination/pagination.component";
import { environment } from "../../../../../../environments/environment";
import * as _ from "lodash";
import { InputModeComponent } from '../../content/input-mode/input-mode.component';
import { RouteModeComponent } from '../../content/route-mode/route-mode.component';
import { BatchModeComponent } from '../../content/batch-mode/batch-mode.component';
import { RemindComponent } from '../../content/remind/remind.component';
import { RecordService } from './record.service';
import { DataService } from 'app/services/data.service';
import { Util } from '../../../../../common/util';
@Component({
  selector: 'app-record',
  templateUrl: './record.component.html',
  styleUrls: ['./record.component.scss'],
  providers:[RecordService]
})
export class RecordComponent implements OnInit {
  // 录入模式
  @ViewChild("inputMode", { read: ViewContainerRef })
  inputMode: ViewContainerRef;
  inputModeComponent: ComponentRef<InputModeComponent>;
  // 在途模式
  @ViewChild("routeMode", { read: ViewContainerRef })
  routeMode: ViewContainerRef;
  routeModeComponent: ComponentRef<RouteModeComponent>;
  // 批次模式
  @ViewChild("batchMode", { read: ViewContainerRef })
  batchMode: ViewContainerRef;
  batchModeComponent: ComponentRef<BatchModeComponent>;
  // 提醒
  @ViewChild("remind", { read: ViewContainerRef })
  remind: ViewContainerRef;
  remindComponent: ComponentRef<RemindComponent>;
  // lodash
  public _: any = _;
  // 菜单数据
  public menuData: any;
  // 菜单权限set
  public menuResourceSet: any = [];

  constructor(
    public resolver: ComponentFactoryResolver,
    public recordService:RecordService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    let that = this;
    this.showInputMode();
    // 权限处理
    this.dataService.initTree.subscribe((data) => {
      if (data) {
        // let SessionMenuData = sessionStorage.getItem('menu');
        // this.menuData = JSON.parse(SessionMenuData)
        // this.menuResourceSet = [];
        // _.forEach(this.menuData, (item, index) => {
        //   if (item.sName == '风控管理') {        
        //     _.forEach(item.children, (it, idx) => {
        //       if (it.sName == '非QDII条目管理') {             
        //         _.forEach(it.children, (ite, i) => {
        //           if (ite.sName == '条目录入') { 
        //             that.menuResourceSet = ite.resourceSet
        //             sessionStorage.setItem('btn-NQDII', this.menuResourceSet);
        //           }
        //         })
        //       }
        //     })
        //   }
        // })  
        this.menuResourceSet = [];
        this.menuResourceSet = Util.getAuthority('风控管理', '非QDII条目管理', '条目录入');
        sessionStorage.setItem('btn-NQDII', this.menuResourceSet);    
      }
    })
    this.menuResourceSet = [];
    this.menuResourceSet = Util.getAuthority('风控管理', '非QDII条目管理', '条目录入');
    sessionStorage.setItem('btn-NQDII', this.menuResourceSet); 
  }
  // 录入模式列表
  showInputMode() {
    // 判断组件是否已创建，如果创建先注销
    if (this.inputModeComponent && this.inputModeComponent.instance) {
      this.inputModeComponent.destroy();
    }
    // 动态创建组件的实例
    const inputModeComponent = this.resolver.resolveComponentFactory(InputModeComponent);
    this.inputModeComponent = this.inputMode.createComponent(inputModeComponent);
  }
  // 在途模式列表
  showRouteMode() {
    // 判断组件是否已创建，如果创建先注销
    if (this.routeModeComponent && this.routeModeComponent.instance) {
      this.routeModeComponent.destroy();
    }
    // 动态创建组件的实例
    const routeModeComponent = this.resolver.resolveComponentFactory(RouteModeComponent);
    this.routeModeComponent = this.routeMode.createComponent(routeModeComponent);
  }
  // 批次模式列表
  showBatchMode() {
    // 判断组件是否已创建，如果创建先注销
    if (this.batchModeComponent && this.batchModeComponent.instance) {
      this.batchModeComponent.destroy();
    }
    // 动态创建组件的实例
    const batchModeComponent = this.resolver.resolveComponentFactory(BatchModeComponent);
    this.batchModeComponent = this.batchMode.createComponent(batchModeComponent);
    this.batchModeComponent.instance.menuResource = this.menuResourceSet;
    // console.log("啊哈哈哈哈")
    // console.log(this.menuResourceSet)
  }
  // 提醒
  showRemind() {
    // 判断组件是否已创建，如果创建先注销
    if (this.remindComponent && this.remindComponent.instance) {
      this.remindComponent.destroy();
    }
    // 动态创建组件的实例
    const remindComponent = this.resolver.resolveComponentFactory(RemindComponent);
    this.remindComponent = this.remind.createComponent(remindComponent);
  }
}
